/**
 * 
 */
package com.mzba.place.adapter;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mzba.place.R;
import com.mzba.place.entity.NotificationEntity;
import com.mzba.place.utils.ImageLoader;
import com.mzba.place.utils.Utils;

/**
 * 列表适配器
 * 
 * @author iris
 * 
 */
public class NotificationListAdapter extends BaseAdapter {

	private List<NotificationEntity> notificationList;
	private LayoutInflater layoutInflater;
	private HashMap<String, String> getNotificationType;
	ImageLoader mImageLoader;

	String distances;

	DecimalFormat df;
	Resources res;
	public NotificationListAdapter(Context context, List<NotificationEntity> notificationList, ImageLoader mImageLoader) {
		this.notificationList = notificationList;
		this.mImageLoader = mImageLoader;
		layoutInflater = LayoutInflater.from(context);
		getNotificationType = Utils.getNotificationType(context);

		res= context.getResources();
		df = new DecimalFormat("########0.00");
	}

	@Override
	public int getCount() {
		return notificationList == null ? 0 : notificationList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return notificationList == null ? null : notificationList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return notificationList.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.fragment_notification_list_item, null);
			holder = new ViewHolder();
			holder.tv_report_type = (TextView) convertView.findViewById(R.id.tv_report_type);
			holder.iv_report_image = (ImageView) convertView.findViewById(R.id.iv_report_image);
			holder.tv_notification_time = (TextView) convertView.findViewById(R.id.tv_notification_time);
			holder.tv_report_recording = (ImageView) convertView.findViewById(R.id.iv_report_recording);
			holder.tv_report_content = (TextView) convertView.findViewById(R.id.tv_report_content);
			holder.iv_is_read = (ImageView) convertView.findViewById(R.id.tv_report_icon);
			holder.ll_content = (LinearLayout) convertView.findViewById(R.id.ll_content);
			
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			NotificationEntity notificationEntity = notificationList.get(position);
			String notify_type = notificationEntity.getNotify_type();
			String  time  = Utils.getCreateAt(notificationEntity.getReport().getCreated_at());
			String notify_text = null;
			holder.tv_notification_time.setText(notificationEntity.getCreated_at());
			if (notify_type.equals("vote")) {
				notify_text= String.format(res.getString(R.string.notification_vote),time);
				getNotificationType.get(notificationEntity.getNotify_type());
				holder.ll_content.setVisibility(View.GONE);
			} else if (notify_type.equals("mine_comment")) {
				notify_text= String.format(res.getString(R.string.notification_mine_comment),time);
				holder.tv_report_content.setText(notificationEntity.getComment() + "");
				holder.ll_content.setVisibility(View.VISIBLE);
			}else if (notify_type.equals("star_comment")) {
				notify_text = res.getString(R.string.notification_star_comment);
				holder.tv_report_content.setText(notificationEntity.getComment() + "");
				holder.ll_content.setVisibility(View.VISIBLE);
			}else if (notify_type.equals("caution")) {
				notify_text = res.getString(R.string.notification_caution);
				holder.ll_content.setVisibility(View.GONE);
			}
			holder.tv_report_type.setText(notify_text);
			boolean isTrue = notificationEntity.isIs_read();
			if (isTrue) {
				holder.iv_is_read.setBackgroundResource(R.drawable.ic_star_red_16);
				holder.iv_is_read.setEnabled(false);
			}else {
				holder.iv_is_read.setBackgroundResource(R.drawable.ic_star_gray_16);
				holder.iv_is_read.setEnabled(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView iv_is_read;
		ImageView tv_report_recording;
		TextView tv_report_type;
		TextView tv_notification_time;
		TextView tv_report_content;
		ImageView iv_report_image;
		LinearLayout ll_content;
	}

	/**
	 * @return the notificationList
	 */
	public List<NotificationEntity> getNotificationList() {
		return notificationList;
	}

	/**
	 * @param notificationList the notificationList to set
	 */
	public void setNotificationList(List<NotificationEntity> notificationList) {
		this.notificationList = notificationList;
	}

}
